package com.wantup.controller;

import com.alibaba.fastjson.JSONObject;
import com.wantup.pojo.RespInfo;
import com.wantup.service.UserService;
import com.wantup.service.impl.UserService01Impl;
import com.wantup.service.impl.UserService02Impl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *
 * </p>
 *
 * @author 赵洛伟
 * @since 2019-06-05
 */
@RestController
@RequestMapping(value = "/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService01Impl userService01;

    @Autowired
    private UserService02Impl userService02;

    @Autowired
    private UserService userService;


    //@RequestMapping(value = "/insertUser")
    public String inserUser(String username, String password) {
        int insertUser = this.userService01.insertUser(username, password);
        if (insertUser > 0) {
            return "success";
        } else {
            return "fail";
        }

    }

    //@RequestMapping(value = "/insertUser2")
    public String inserUser2(String username, String password) {
        int insertUser = this.userService02.insertUser(username, password);
        if (insertUser > 0) {
            return "success";
        } else {
            return "fail";
        }

    }

    @RequestMapping(value = "/add")
    public String add(String username, String password) {
        try {
            int insertUser = this.userService.insertUser(username, password);
            if (insertUser > 0) {
                return "success";
            } else {
                return "fail";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "fail";
        }

    }


    @RequestMapping(value = "/changeData", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespInfo changeData(@RequestBody JSONObject jsonObject) {
        RespInfo respInfo = new RespInfo();
        try {
            Integer id = jsonObject.getInteger("id");
            log.info("入参 --> id = " + id);
            int insertUser = this.userService.changeDataService(id);
            if (insertUser > 0) {
                return respInfo.setCode(0).setMessage("操作成功").setData("");
            } else {
                return respInfo.setCode(1).setMessage("操作失败").setData("");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return respInfo.setCode(-1).setMessage("操作异常").setData("");
        }

    }


}
